using System.Linq;
using UnityEngine;
using UnityEngine.UI;

namespace MaterialUI
{
	[AddComponentMenu("MaterialUI/Tab Page", 100)]
	public class TabPage : MonoBehaviour
	{
		[SerializeField]
		private bool m_Interactable = true;

		[SerializeField]
		private bool m_DisableWhenNotVisible = true;

		[SerializeField]
		private bool m_ShowDisabledPanel = true;

		[SerializeField]
		private string m_TabName;

		[SerializeField]
		private ImageData m_TabIcon;

		private CanvasGroup m_CanvasGroup;

		private RectTransform m_RectTransform;

		private TabView m_TabView;

		private RectTransform m_DisabledPanel;

		private bool m_LastEnabled = true;

		public bool interactable
		{
			get
			{
				return m_Interactable;
			}
			set
			{
				m_Interactable = value;
			}
		}

		public bool disableWhenNotVisible
		{
			get
			{
				return m_DisableWhenNotVisible;
			}
			set
			{
				m_DisableWhenNotVisible = value;
			}
		}

		public bool showDisabledPanel
		{
			get
			{
				return m_ShowDisabledPanel;
			}
			set
			{
				m_ShowDisabledPanel = value;
			}
		}

		public string tabName
		{
			get
			{
				return m_TabName;
			}
			set
			{
				m_TabName = value;
			}
		}

		public ImageData tabIcon
		{
			get
			{
				return m_TabIcon;
			}
			set
			{
				m_TabIcon = value;
			}
		}

		private CanvasGroup canvasGroup
		{
			get
			{
				if (m_CanvasGroup == null)
				{
					m_CanvasGroup = tabView.tabs[tabView.pages.ToList().IndexOf(this)].GetAddComponent<CanvasGroup>();
				}
				return m_CanvasGroup;
			}
		}

		public RectTransform rectTransform
		{
			get
			{
				if (m_RectTransform == null)
				{
					m_RectTransform = (RectTransform)base.transform;
				}
				return m_RectTransform;
			}
		}

		private TabView tabView
		{
			get
			{
				if (m_TabView == null)
				{
					m_TabView = GetComponentInParent<TabView>();
				}
				return m_TabView;
			}
		}

		private void Update()
		{
			if (m_LastEnabled == m_Interactable)
			{
				return;
			}
			m_LastEnabled = m_Interactable;
			if (!(canvasGroup != null))
			{
				return;
			}
			if (m_LastEnabled)
			{
				canvasGroup.blocksRaycasts = true;
				canvasGroup.alpha = 1f;
				if (m_DisabledPanel != null)
				{
					UnityEngine.Object.Destroy(m_DisabledPanel.gameObject);
				}
				return;
			}
			canvasGroup.blocksRaycasts = false;
			canvasGroup.alpha = 0.15f;
			if (m_ShowDisabledPanel)
			{
				m_DisabledPanel = PrefabManager.InstantiateGameObject("DisabledPanel", rectTransform).GetComponent<RectTransform>();
				m_DisabledPanel.anchoredPosition = Vector2.zero;
				m_DisabledPanel.sizeDelta = Vector2.zero;
				Color color = tabView.GetComponent<Image>().color;
				m_DisabledPanel.GetComponent<Image>().color = color;
				m_DisabledPanel.GetComponentInChildren<Text>().color = ((!((color.r + color.g + color.b) / 3f > 0.8f)) ? MaterialColor.textLight : MaterialColor.textDark);
			}
		}

		public void DisableIfAllowed()
		{
			if (m_DisableWhenNotVisible)
			{
				base.gameObject.SetActive(value: false);
			}
		}
	}
}
